/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package adminpaper;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSetMetaData;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class statisticframe extends javax.swing.JFrame implements ItemListener {

    /**
     * Creates new form statisticframe
     */
    
    JTable table=new JTable();
    String year="2016-2017";String semester="第一学期";
    DATABASE.DataBase db=new DATABASE.DataBase();
     String sql="select 学院,sum(试卷份数)as 试卷总数  from paper where 学年=? and 学期=? group by 学院";
    public statisticframe() {
        initComponents();
        setLocationRelativeTo(null);
        setVisible(true);
        
        jComboBox1.addItemListener(this);
        jComboBox2.addItemListener(this);
        
        //showTable(sql,year,semester,5,40,370,300);
            setTable(sql,year,semester);
            JScrollPane scroll = new JScrollPane(table);
            scroll.setBounds(5, 40,370, 300);
            //scroll.setBorder(new TitledBorder("借阅信息"));
            getContentPane().add(scroll);
            scroll.setOpaque(false);
            scroll.getViewport().setOpaque(false);
            table.setOpaque(false);
        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                    dispose();
                    }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox<>();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("统计");
        setBackground(new java.awt.Color(51, 204, 255));
        setForeground(new java.awt.Color(0, 255, 255));

        jLabel1.setText("学年");
        jLabel1.setOpaque(true);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "2016-2017", "2015-2016", "2014-2015" }));

        jLabel2.setText("学期");
        jLabel2.setOpaque(true);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "第一学期", "第二学期" }));

        jButton1.setText("统计");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(328, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void itemStateChanged(ItemEvent e){
           if(e.getSource()==jComboBox1){
               if(e.getStateChange()==ItemEvent.SELECTED){
                  year=(String) jComboBox1.getSelectedItem();
               }
           }
           if(e.getSource()==jComboBox2){
                if(e.getStateChange()==ItemEvent.SELECTED){
                   semester=(String) jComboBox2.getSelectedItem();
                }
           }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       DefaultTableModel model=(DefaultTableModel) table.getModel();
       for(int i=0;i<table.getRowCount();i++)
       model.removeRow(i);
            
       this.setTable(sql,year,semester);
       
       
    }//GEN-LAST:event_jButton1ActionPerformed
    public void setTable(String sql,String year,String semester) {
        try {
            DefaultTableModel model=null;
            model= db.getResultSet1(sql, year, semester);
            table.setModel(model);
//            table.repaint();
//            table.updateUI();
        } catch (SQLException ex) {
            Logger.getLogger(statisticframe.class.getName()).log(Level.SEVERE, null, ex);
        }
    
      } 
    
    public void showTable(String sql,String year,String semester,int x,int y,int width,int height){

            
            JScrollPane scroll = new JScrollPane(table);
            scroll.setBounds(x, y, width, height);
            //scroll.setBorder(new TitledBorder("借阅信息"));
            getContentPane().add(scroll);
            scroll.setOpaque(false);
            scroll.getViewport().setOpaque(false);
            table.setOpaque(false);
         
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
           }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(statisticframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(statisticframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(statisticframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(statisticframe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
//        //</editor-fold>
        /* Create and display the form */
      
                new statisticframe();
   }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
